package com.yazi.mxz.common.service.impl;


import javax.annotation.Resource;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import com.myframework.smhj.common.tool.DateUtil;
import com.myframework.smhj.hibernate.Searching;
import com.myframework.smhj.hibernate.Page;
import com.myframework.smhj.hibernate.dao.BasicService;
import com.myframework.smhj.hibernate.dao.Dao;
import com.yazi.mxz.common.client.ActFactory;
import com.yazi.mxz.common.db.dao.EmailDao;
import com.yazi.mxz.common.entity.ValueFactory;
import com.yazi.mxz.common.entity.beans.Email;
import com.yazi.mxz.common.service.EmailService;

@Service("emailService")
public class EmailServiceImpl extends BasicService<Email> implements EmailService {
	private static Logger log = LoggerFactory.getLogger(EmailServiceImpl.class);
	
	@Resource
	EmailDao emailDao;
	
	@Override
	protected Dao<Email> getDao() {
		return emailDao;
	}
	
	@Override
	public Email add(Email email) {
		log.debug("add: toEmail={},type={}", email.getToEmail(), email.getType());
		
		ValueFactory.validate(email);
		email.setEmailId(null);
		email.setAddTime(DateUtil.getTimeStamp());
		email = emailDao.persist(email);
		
		// 触发邮件发送
		ActFactory.actEmailJob();
		
		return email;
	}
	
	@Override
	public Email edit(Email email) {
		log.debug("edit: emailId={}", email.getEmailId());
		
		Email dstEmail = emailDao.get(email.getEmailId());
		ValueFactory.combine(email, dstEmail);
		
		return emailDao.merge(email);
	}
	
	@Override
	public Page<Email> listByGtAddtime(Email email, Searching search) {
		log.debug("listByGtAddtime：");
		return emailDao.listByGtAddtime(email, search);
	}
	
}
